﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Net;

using Factonomy.AzureStorageUtility.WindowsApp.Resources;
using Factonomy.AzureStorageUtility.WindowsApp.Azure;

namespace Factonomy.AzureStorageUtility.WindowsApp.UI.Forms
{
    public partial class AddAccount : Form
    {
        public AddAccount()
        {
            InitializeComponent();

            // set the window icon
            this.Icon = ResourceManager.GetAppIcon();

            this.uiButtonSave.Click += new EventHandler(uiButtonSave_Click);
        }

        void uiButtonSave_Click(object sender, EventArgs e)
        {
            BlobManager manager = new BlobManager();

            if (manager.Test(this.uiTextboxAccountName.Text, new Uri("http://blob.core.windows.net"), false, this.uiTextboxSharedKey.Text))
            {
                SaveConfig();

                this.Hide();

                Main main = new Main();
                main.Owner = this;
                main.Show();
            }
            else
            {
                MessageBox.Show("Invalid account details.  Please try again.");
            }
        }

        private void SaveConfig()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.AppSettings.Settings["init"] != null)
            {
                config.AppSettings.Settings.Remove("init");
            }

            // add or update the default account
            if (config.AppSettings.Settings[Globals.DefaultAccountSettingName] == null)
            {
                config.AppSettings.Settings.Add(Globals.DefaultAccountSettingName, this.uiTextboxAccountName.Text);
            }
            else
            {
                config.AppSettings.Settings[Globals.DefaultAccountSettingName].Value = this.uiTextboxAccountName.Text;
            }

            // add or update the default account
            if (config.AppSettings.Settings["accounts"] == null)
            {
                config.AppSettings.Settings.Add("accounts", this.uiTextboxAccountName.Text);
            }
            else
            {
                config.AppSettings.Settings["accounts"].Value = config.AppSettings.Settings["accounts"].Value + "|" + this.uiTextboxAccountName.Text;
            }

            config.AppSettings.Settings.Add(this.uiTextboxAccountName.Text + ".AccountSharedKey", this.uiTextboxSharedKey.Text);
            config.AppSettings.Settings.Add(this.uiTextboxAccountName.Text + ".BlobStorageEndpoint", "http://blob.core.windows.net");
            config.AppSettings.Settings.Add(this.uiTextboxAccountName.Text + ".QueueStorageEndpoint", "http://queue.core.windows.net");
            config.AppSettings.Settings.Add(this.uiTextboxAccountName.Text + ".TableStorageEndpoint", "http://table.core.windows.net");
            config.AppSettings.Settings.Add(this.uiTextboxAccountName.Text + ".UsePathStyleUris", "false");

            config.Save(ConfigurationSaveMode.Full);

            ConfigurationManager.RefreshSection("appSettings");
        }

    }
}
